package com.act;

import java.util.ArrayList;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import com.bean.BaseBean;
import com.common.Constant;
import com.common.TextHttpResponseHandler;
import com.control.UserControl;
import com.xike.kyt.R;
import com.loopj.android.http.RequestParams;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

import android.app.DownloadManager.Request;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class Friend_AddAct extends BaseAct implements OnClickListener {
	TextView tv_back;
	TextView tv_send;
	EditText edt_info;
	long user_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_friend_add);

		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.containsKey("user_id")) {
			user_id = bundle.getLong("user_id");
		}

		initViews();
	}

	@Override
	protected void initViews() {
		tv_back = (TextView) findViewById(R.id.headTitle_tv_back);
		tv_back.setOnClickListener(this);
		tv_send = (TextView) findViewById(R.id.addFriend_tv_send);
		tv_send.setOnClickListener(this);

		edt_info = (EditText) findViewById(R.id.addFriend_edt_info);
		edt_info.setText("我是" + UserControl.getLoginUserName());
		edt_info.setSelection(edt_info.getText().toString().length());
	}

	@Override
	protected void updateViews() {

	}

	private void serviceAddFriend() {
		String str_info = MyUtil.getEditTextContent(edt_info);
		if (!StringUtil.isNotNull(str_info)) {
			mToast.showToast(this, "请输入申请验证信息");
			return;
		}
		ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("friend_id", String.valueOf(user_id)));
		params.add(new BasicNameValuePair("info", str_info));

		// 网络请求
		client.post(Constant.SERVER_URL_FRIENDS_APPLY, MyUtil.getPostRequestParams(params), new TextHttpResponseHandler(this) {

			@Override
			public void onSuccess(int statusCode, Header[] headers, String responseString) {
				BaseBean result = gson.fromJson(responseString, BaseBean.class);
				if (result != null && result.isReturnTrue()) {
					mToast.showToast(Friend_AddAct.this, "请求发送成功");
					finish();
				} else {
					mToast.showToast(Friend_AddAct.this, getResources().getString(R.string.net_error));
				}

				super.onSuccess(statusCode, headers, responseString);
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
				mToast.showToast(Friend_AddAct.this, getString(R.string.net_error));
				super.onFailure(statusCode, headers, responseString, throwable);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.headTitle_tv_back:
			finish();
			break;
		case R.id.addFriend_tv_send:
			serviceAddFriend();
			break;

		default:
			break;
		}
	}

}
